<template>
  <div>
    <el-container style="background: #f5f7f9;">
      <el-header style="background: #fff;padding: 12px 12px;height:auto;">
        <div>
          <el-form :model="queryData" inline size="small">
            <el-form-item>
              <el-select v-model="queryData.enable">
                <el-option value="true" label="启用" />
                <el-option value="false" label="停用" />
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="list">搜索</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-header>
      <el-main style="margin-top: 15px; background: #fff;padding: 12px 16px 0 16px;height: 100%;">
        <div style="height: 100%;margin-bottom: 12px;display: flex;">
          <el-button v-has="50101" type="primary" @click="add">新增</el-button>
        </div>
        <el-table :data="table_body" stripe :max-height="tableHeight" border>
          <el-table-column prop="channelCode" label="渠道代码" />
          <el-table-column prop="channelName" label="渠道名称" />
          <el-table-column prop="enable" label="状态">
            <template slot-scope="scope">
              <div v-if="scope.row.enable">启用</div>
              <div v-else>停用</div>
            </template>
          </el-table-column>
          <el-table-column prop="operator" label="操作者" />
          <el-table-column prop="createAt" label="创建时间" />
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button v-has="50102" size="small" shape="circle" type="text" @click="edit(scope.row)">编辑</el-button>
              <el-button v-if="scope.row.enable" v-has="50103" size="small" shape="circle" type="text" @click="disabled(scope.row)">停用</el-button>
              <el-button v-else v-has="50104" size="small" shape="circle" type="text" @click="enable(scope.row)">启用</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-main>
      <el-footer style="padding:0;background: #fff;">
        <div style="text-align: right; padding-top: 10px;">
          <el-pagination background :current-page="queryData.currentPage" :page-sizes="[50,100]" :page-size="queryData.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="pageTotal" style="float:right;" @size-change="onPageSizeChange" @current-change="onPageChange" />
        </div>
      </el-footer>
    </el-container>
    <New ref="channelNew" @query="list" />
  </div>
</template>
<script>
import request from '@/utils/request'
import New from './new'
export default {
  components: {
    New
  },
  data() {
    return {
      table_body: [],
      queryData: {
        pageSize: 50,
        currentPage: 0,
        enable: 'true'
      },
      pageTotal: 0,
      pageSizeOpts: [50, 100]
    }
  },
  created() {
    this.list()
  },
  mounted() {
  },
  methods: {
    edit(row) {
      this.$refs.channelNew.edit(row)
    },
    add() {
      this.$refs.channelNew.add()
    },
    list() {
      request({
        url: 'product/query',
        data: this.queryData,
        method: 'post'
      }).then(res => {
        if (res.success) {
          if (res.data) {
            this.table_body = res.data.records
            this.pageTotal = res.data.total
          } else {
            this.pageTotal = 0
          }
        }
      })
    },
    enable(row) {
      request({
        url: 'product/enable?id=' + row.id,
        method: 'GET'
      }).then(res => {
        if (res.success) {
          this.list()
        }
      })
    },
    disabled(row) {
      const _this = this
      this.$confirm('确认要停用吗', '提示', {
        confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
      }).then(() => {
        request({
          url: 'product/disabled?id=' + row.id,
          method: 'GET'
        }).then(res => {
          if (res.success) {
            _this.list()
          }
        })
      })
    },
    onPageSizeChange(pageSize) {
      this.queryData.pageSize = pageSize
      this.list()
    },
    onPageChange(page) {
      this.queryData.currentPage = page
      this.list()
    }
  },
  computed: {
    tableHeight() {
      return document.documentElement.clientHeight - 300
    },
    pageSize() {
      return this.pageSizeOpts[0]
    }
  }
}
</script>
